import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import OAuth2Window from "./utils";
import authConfig from "./config";
import { HmacSHA1, enc } from "crypto-js";
import { getTwitterAuthUrl } from "../api/auth";
var TwitterAuth = /*#__PURE__*/ function _target() {
    "use strict";
    function TwitterAuth() {
        var _this = this;
        _class_call_check(this, TwitterAuth);
        _define_property(this, "load", true);
        var _this1 = this;
        _define_property(this, "obtainOauthRequestToken", /*#__PURE__*/ _async_to_generator(function() {
            var oauthSignature, res;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        oauthSignature = _this1.requestTokenSignature();
                        console.log(oauthSignature);
                        return [
                            4,
                            fetch("https://api.allorigins.win/get?url=".concat(encodeURIComponent("https://api.twitter.com/oauth/request_token")), {
                                method: "POST",
                                headers: {
                                    Authorization: "OAuth ".concat(oauthSignature)
                                }
                            })
                        ];
                    case 1:
                        res = _state.sent();
                        return [
                            2
                        ];
                }
            });
        }));
        _define_property(this, "requestTokenSignature", function() {
            var params = {
                oauth_consumer_key: authConfig.TWITTER_CONSUMER_KEY,
                oauth_version: "1.0",
                oauth_signature_method: "HMAC-SHA1",
                oauth_callback: authConfig.REDIRECT_URI,
                oauth_timestamp: (Date.now() / 1000).toFixed(),
                oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, "").substr(2)
            };
            return _this.makeSignature(params, "POST", "https://api.twitter.com/oauth/request_token", authConfig.TWITTER_CONSUMER_SECRET);
        });
        _define_property(this, "makeSignature", function(params, method, apiUrl, consumerSecret) {
            var paramsBaseString = Object.keys(params).sort().reduce(function(prev, el) {
                return prev += "&".concat(el, "=").concat(params[el]);
            }, "").substr(1);
            var signatureBaseString = "".concat(method.toUpperCase(), "&").concat(apiUrl, "&").concat(paramsBaseString);
            var signingKey = "".concat(consumerSecret, "&");
            var oauth_signature = enc.Base64.stringify(HmacSHA1(signatureBaseString, signingKey));
            var paramsWithSignature = _object_spread_props(_object_spread({}, params), {
                oauth_signature: encodeURIComponent(oauth_signature)
            });
            return Object.keys(paramsWithSignature).sort().reduce(function(prev, el) {
                return prev += ",".concat(el, '="').concat(paramsWithSignature[el], '"');
            }, "").substr(1);
        });
        _define_property(this, "getCode", function() {
            return new Promise(function(resolve, reject) {
                var options = {
                    redirect_uri: authConfig.REDIRECT_URI,
                    client_id: authConfig.TWITTER_CLIENTID,
                    state: "twitter",
                    response_type: "code",
                    code_challenge: "challenge",
                    code_challenge_method: "plain",
                    scope: [
                        "users.read",
                        "offline.access",
                        "tweet.read"
                    ].join(" ")
                };
                OAuth2Window.open("twitter", "https://twitter.com/i/oauth2/authorize?".concat(new URLSearchParams(options).toString()), {
                    width: "600",
                    height: "700"
                }, "broadcastChannel").then(function(res) {
                    resolve(res);
                }).catch(function(err) {
                    reject(err);
                });
            });
        });
        _define_property(this, "getOauth1Url", /*#__PURE__*/ _async_to_generator(function() {
            var url;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            getTwitterAuthUrl({
                                onaAppId: authConfig.ONA_APPID,
                                onaAppSecret: authConfig.ONA_APP_SECRET
                            })
                        ];
                    case 1:
                        url = _state.sent();
                        if (url.code !== 200) throw url;
                        return [
                            2,
                            url.data
                        ];
                }
            });
        }));
        var _this2 = this;
        _define_property(this, "getOauth1Code", /*#__PURE__*/ _async_to_generator(function() {
            return _ts_generator(this, function(_state) {
                return [
                    2,
                    new Promise(function(resolve, reject) {
                        try {
                            var twitterPopup = OAuth2Window.open("twitter", "", {
                                width: "600",
                                height: "700"
                            });
                            _this2.getOauth1Url().then(function(uri) {
                                twitterPopup.setUrl(uri).then(function(res) {
                                    resolve(res);
                                }).catch(function(err) {
                                    return reject(err);
                                });
                            }).catch(function(urlErr) {
                                twitterPopup.close();
                                reject(urlErr.message);
                            });
                        } catch (err) {
                            reject(err.message || err);
                        }
                    })
                ];
            });
        }));
        _define_property(this, "parseOAuth1Response", function(responseText) {
            responseText.split("&").reduce(function(prev, el) {
                var _el_split = _sliced_to_array(el.split("="), 2), key = _el_split[0], value = _el_split[1];
                prev[key] = value;
                return prev;
            }, {});
        });
        if ((_instanceof(this, TwitterAuth) ? this.constructor : void 0) !== TwitterAuth) {
            return;
        }
        if (!TwitterAuth._instance) {
            TwitterAuth._instance = this;
        }
        return TwitterAuth._instance;
    }
    _create_class(TwitterAuth, [
        {
            key: "init",
            value: function init() {
                return new Promise(function(resolve, reject) {
                    resolve(TwitterAuth._instance);
                });
            }
        }
    ], [
        {
            key: "getInstance",
            value: function getInstance() {
                if (!TwitterAuth._instance) {
                    TwitterAuth._instance = new TwitterAuth();
                }
                return TwitterAuth._instance;
            }
        }
    ]);
    return TwitterAuth;
}();
_define_property(TwitterAuth, "_instance", null);
// const instance = TwitterAuth.getInstance();
export default TwitterAuth.getInstance();

 //# sourceMappingURL=twitter.js.map